package com.lwgk.mvp.model;

import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;

import com.lwgk.AppComplication;
import com.lwgk.constant.ConstantUtils;
import com.lwgk.mvp.presenter.SplashPresentImpl;
import com.lwgk.mvp.presenter.SplashView;
import com.lwgk.utils.SaveDataUtils;
import com.lwgk.utils.StatusBarUtils;

/**
 * created by 赵航
 * 联系微信 微信号：zhao--hang--
 * on 2019/7/11
 * --------------------------------------------------------------------------
 * 描述：闪屏界面的实现业务逻辑层
 * 注意事项：
 */
public class SplashPresent implements SplashPresentImpl {

    private Handler    mHandler = new Handler(Looper.myLooper());
    //MVP第二步，采用多肽形式进行接收，不要直接用实体类接收
    private SplashView mSplashView;

    public SplashPresent(SplashView splashActivity) {
        this.mSplashView = splashActivity;
        //闪屏时就获取actionBar的高度
        if (AppComplication.mActionBarSize==0) {
            AppComplication.mActionBarSize= StatusBarUtils.getActionBarHeight();
        }
    }

    //MVP第四步处理业务逻辑
    @Override
    public void checkActionSkip(Intent action) {
        //先进行判空
        if (action == null) {
            //不是从其他界面过来的操作
            noActionDo();
        } else {

            if (Intent.ACTION_VIEW.equals(action)) {
                Uri uri = action.getData();
                if (uri != null) {
                    //参加拼团的Uri  lwgk://gbid='+href().id+'&type='+new_type+'&kcxq='+ids
                    //参加拼团成功后的的uri  lwgk://
                    String authority = uri.getAuthority();
                    if (authority.contains("=")) {
                        //                        String substring = authority.substring(0, authority.indexOf("&"));
                        //                        String substring1 = authority.substring(authority.indexOf("&"));
                        String[] split = authority.split("&");
                        String s = split[0];
                        String s1 = split[1];
                        String[] split1 = s.split("=");
                        String s2 = split1[1];
                        String[] split2 = s1.split("=");
                        String s3 = split2[1];
                        //                        if (split1[0].equals("kcxq")) {
                        //                            if (s3.equals("2")) {
                        //                                Intent intent = new Intent(SplashActivity.this, FacetoFaceActivity.class);
                        //                                intent.putExtra("id", Integer.valueOf(s2));
                        //                                intent.putExtra("type", Integer.valueOf(s3));
                        //                                startActivity(intent);
                        //                                finish();
                        //                            } else {
                        //                                Intent intent = new Intent(SplashActivity.this, LiveDetailsActivity.class);
                        //                                intent.putExtra("id", Integer.valueOf(s2));
                        //                                intent.putExtra("type", Integer.valueOf(s3));
                        //                                startActivity(intent);
                        //                                finish();
                        //                            }
                        //                        } else {
                        //                            Intent intent = new Intent(SplashActivity.this, CollageSuccessActivity.class);
                        //                            intent.putExtra("sign", 3);
                        //                            intent.putExtra("id", Integer.valueOf(s2));
                        //                            intent.putExtra("collage_type", Integer.valueOf(s3));
                        //                            startActivity(intent);
                        //                            finish();
                        //                        }

                    } else {
                        //不是从其他界面过来的操作
                        noActionDo();
                    }
                } else {
                    //不是从其他界面过来的操作
                    noActionDo();
                }
            } else {
                //不是从其他界面过来的操作
                noActionDo();
            }


        }
    }

    private void noActionDo() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //判断当前是否首次打开App
                boolean isNewInstall = (boolean) SaveDataUtils.getData(ConstantUtils.SMOOTHPLATE, false);
                //MVP第五步，处理完逻辑后，进行方法回调
                //说明是正常打开的app，
                mSplashView.openNormalApp(isNewInstall);
            }
        }, ConstantUtils.SPLASHWITETIME);
    }
}
